package com.purplesoft.zettl.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.purplesoft.zettl.DatabaseTables.TblCategories;
import com.purplesoft.zettl.DatabaseTables.TblItems;
import com.purplesoft.zettl.DatabaseTables.TblManufacturer;
import com.purplesoft.zettl.DatabaseTables.TblMeasures;
import com.purplesoft.zettl.DatabaseTables.TblPurchase;
import com.purplesoft.zettl.DatabaseViews.VwItems;
import com.purplesoft.zettl.DatabaseViews.VwPurchaseItems;

public class DatabaseManager {

	// private string with database name
	private static final String DatabaseName = "zettl.db";
	// private integer with database version
	private static final int DatabaseVersion = 1;

	// private variable for database object
	private SQLiteDatabase database;
	// private variable for database open helper object
	private DatabaseOpenHelper databaseHelper;

	private static final String TAG = DatabaseManager.class.getName();

	// public constructor for DatabaseManager
	public DatabaseManager(Context context) {
		// create an new DatabaseOpenHelper
		databaseHelper = new DatabaseOpenHelper(context, DatabaseName, null,
				DatabaseVersion);
	}

	// open the database connection
	public void open() throws SQLException {
		// create or open a database for reading/writing
		database = databaseHelper.getWritableDatabase();
	}

	// close the database connection
	public void close() {
		if (database != null)
			database.close(); // close the database connection
	}

	// public cursor that returns the whole table "tbl_items"
	public Cursor selectVwPurchaseItems(String[] columns, String whereClause,
			String[] whereClauseArgs, String orderBy) {
		try {
			open();
			return database.query(VwPurchaseItems.viewName, columns, whereClause, whereClauseArgs,
					null, null, orderBy);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
			return null;
		}
	}

	// public cursor that returns the whole table "tbl_items"
	public Cursor selectVwItems(String[] columns, String whereClause,
			String[] whereClauseArgs, String orderBy) {
		return database.query(VwItems.viewName, columns, whereClause, whereClauseArgs,
				null, null, orderBy);
	}
	// public cursor that returns the whole table "tbl_items"
	public Cursor selectTblPurchase(String[] columns, String whereClause,
			String[] whereClauseArgs, String orderBy) {
		return database.query(TblPurchase.tableName, columns, whereClause, whereClauseArgs, null, null, orderBy);
	}
	
	public Cursor selectTblCategories(String[] columns, String whereClause,
			String[] whereClauseArgs, String orderBy) {
		return database.query(TblCategories.tableName, columns, whereClause, whereClauseArgs, null, null, orderBy);
	}

	public Cursor selectTblItems(String[] columns, String whereClause,
			String[] whereClauseArgs, String orderBy) {
		return database.query(TblItems.tableName, columns, whereClause, whereClauseArgs, null, null, orderBy);
	}
	
	public Cursor selectTblManufacturers(String[] columns, String whereClause,
			String[] whereClauseArgs, String orderBy) {
		return database.query(TblManufacturer.tableName, columns, whereClause, whereClauseArgs, null, null, orderBy);
	}
	
	public Cursor selectTblMeasures(String[] columns, String whereClause,
			String[] whereClauseArgs, String orderBy) {
		return database.query(TblMeasures.tableName, columns, whereClause, whereClauseArgs, null, null, orderBy);
	}
	
	// public cursor that returns the whole table "tbl_items"
	public void updateTblItems(ContentValues contentValues, String whereClause, String[] whereClauseArgs) {
		try {
			database.update(TblItems.tableName, contentValues, whereClause, whereClauseArgs);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
	
	// public cursor that returns the whole table "tbl_items"
	public void updateTblPurchase(ContentValues contentValues, String whereClause, String[] whereClauseArgs) {
		try {
			database.update(TblPurchase.tableName, contentValues, whereClause, whereClauseArgs);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
	
	public void updateTblCategories(ContentValues contentValues,
			String whereClause, String[] whereClauseArgs) {
		try {
			database.update(TblCategories.tableName, contentValues, whereClause, whereClauseArgs);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
	
	public void updateTblManufacturers(ContentValues contentValues,
			String whereClause, String[] whereClauseArgs) {
		try {
			database.update(TblManufacturer.tableName, contentValues, whereClause, whereClauseArgs);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
	
	public void updateTblMeasures(ContentValues contentValues,
			String whereClause, String[] whereClauseArgs) {
		try {
			database.update(TblMeasures.tableName, contentValues, whereClause, whereClauseArgs);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
	
	// public cursor that returns the whole table "tbl_items"
	public void insertTblItmes(ContentValues contentValues) {
		try {
			database.insert(TblItems.tableName, null, contentValues);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
	
	// public cursor that returns the whole table "tbl_items"
	public void insertTblPurchase(ContentValues contentValues) {
		try {
			database.insert(TblPurchase.tableName, null, contentValues);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}

	public void insertTblCategories(ContentValues contentValues) {
		try {
			database.insert(TblCategories.tableName, null, contentValues);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
	
	public void insertTblManufacturers(ContentValues contentValues) {
		try {
			database.insert(TblManufacturer.tableName, null, contentValues);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
	
	public void insertTblMeasures(ContentValues contentValues) {
		try {
			database.insert(TblMeasures.tableName, null, contentValues);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}

	public void deleteTblCategories(String whereClause, String[] whereClauseArgs) {
		try {
			database.delete(TblCategories.tableName, whereClause, whereClauseArgs);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}

	public void deleteTblManufacturers(String whereClause,
			String[] whereClauseArgs) {
		try {
			database.delete(TblManufacturer.tableName, whereClause, whereClauseArgs);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}

	public void deleteTblMeasures(String whereClause, String[] whereClauseArgs) {
		try {
			database.delete(TblMeasures.tableName, whereClause, whereClauseArgs);
		} catch (SQLException e) {
			Log.d(TAG, "Cursor query() " + e);
		}
	}
}
